dnl Process this file with autoconf to produce a configure script.

AC_INIT(ht,[2.1.1],[https://sourceforge.net/tracker/?group_id=1066])
AC_PREREQ(2.59)

dnl Check the system.
AC_CANONICAL_SYSTEM

AC_ARG_ENABLE(x11-textmode, 
	[  --enable-x11-textmode   build with x11 textmode support [default=yes]],,enable_x11_textmode="yes")
AC_ARG_ENABLE(maintainermode,
	[  --enable-maintainermode minimise autodetection for package mantainers [default=no]],,enable_maintainermode="no")
AC_ARG_ENABLE(release,
	[  --enable-release        make a release build [default=yes]],,enable_release="yes")
AC_ARG_ENABLE(profiling,
	[  --enable-profiling      make a profile build [default=no]],,enable_profiling="no")

if test "x$enable_maintainermode" = "xno"; then
if test "x$enable_release" = "xyes"; then
    FLAGS_ALL="-O3 -fomit-frame-pointer -Wall -fsigned-char"
else
    FLAGS_ALL="-ggdb -g3 -O0 -Wall -fsigned-char"
fi

if test "x$enable_profiling" = "xyes"; then
    FLAGS_ALL="$FLAGS_ALL -pg -g"
fi
fi

dnl FLAGS_ALL="$FLAGS_ALL -fsanitize=address"

AM_INIT_AUTOMAKE([foreign])
AC_CONFIG_HEADERS([config.h])

dnl Check OS specifica

NEED_CURSES=0
HAVE_PIPE=0
HT_X11BASE="-L/usr/X11/lib -L/usr/X11R6/lib"

case "$target_os" in
linux*) 
	IO_DIR=posix
	echo "*** LINUX, building POSIX version. we need (n)curses." 1>&2
	NEED_CURSES=1
	HAVE_PIPE=1
	FLAGS_ALL="$FLAGS_ALL -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
;;
*freebsd*) 
	IO_DIR=posix
	echo "*** FREEBSD, building native FreeBSD version. we need (n)curses." 1>&2
	NEED_CURSES=1
	HAVE_PIPE=1
	FLAGS_ALL="$FLAGS_ALL -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
;;
*openbsd*) 
	IO_DIR=posix
	echo "*** OpenBSD, building native OpenBSD version. we need (n)curses." 1>&2
	NEED_CURSES=1
	HAVE_PIPE=1
	HT_X11BASE="-L${X11BASE}/lib"
	FLAGS_ALL="$FLAGS_ALL -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
;;
cygwin*)
	IO_DIR=win32
	echo "*** CYGWIN, building native Win32 version (no cygwin.dll)" 1>&2
	FLAGS_ALL="$FLAGS_ALL -mno-cygwin"
	NEED_CURSES=0
	HAVE_PIPE=1
;;
mingw32*)
	IO_DIR=win32
	echo "*** MINGW32, building native Win32 version" 1>&2
	HT_LDFLAGS="$HT_LDFLAGS -static-libgcc -static-libstdc++"
	NEED_CURSES=0
	HAVE_PIPE=1
;;
*) 
	echo "configure: warning: $target_os: unknown target OS, assuming POSIX-compatible..." 1>&2
	IO_DIR=posix
	echo "*** POSIX-generic, we need (n)curses, '-pipe' disabled for safety" 1>&2
	NEED_CURSES=1
	HAVE_PIPE=0
	FLAGS_ALL="$FLAGS_ALL -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
;;	
esac

if test "x$HAVE_PIPE" = "x1"; then
	FLAGS_PIPE="-pipe";
fi

CXXFLAGS="-DNOMACROS $FLAGS_PIPE $FLAGS_ALL  -std=c++14 -Woverloaded-virtual -Wnon-virtual-dtor $CXXFLAGS"
CFLAGS="-DNOMACROS $FLAGS_PIPE $FLAGS_ALL $CFLAGS"

AC_DEFINE_UNQUOTED(SYSTEM_OSAPI_SPECIFIC_TYPES_HDR, "io/$IO_DIR/types.h", [Location of io/$IO_DIR/types.h])

AM_CONDITIONAL(USE_POSIX_DIR, test x$IO_DIR = xposix)
AM_CONDITIONAL(USE_WIN32_DIR, test x$IO_DIR = xwin32)

dnl Checks for programs.
AC_PROG_RANLIB
AC_PATH_PROGS(MAKE, gmake make, :)
AC_PROG_MAKE_SET
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AC_PROG_YACC
AM_PROG_LEX
AM_PROG_AR

if test "x$AR" = "x"; then
	   AC_MSG_ERROR([*** 'ar' and 'gar' missing, please install one of them or fix your \$PATH ***])
fi

if test "x$enable_maintainermode" = "xno"; then
if test "${GXX}" = yes; then
	gxx_version=`${CXX} -v 2>&1 | grep "^.*g.. version" | \\
	sed -e 's/^.*g.. version *//'`
	case ${gxx_version} in
	1.*|2.*|3.*|4.0|4.1|4.2|4.3)
	;;
	*)
	    FLAGS_ALL="$FLAGS_ALL -march=native"
	;;
	esac
fi
fi

dnl Checks for libraries.

minilzo_enabled=no

AC_ARG_WITH(included-lzo,
	AS_HELP_STRING([--with-included-lzo],
		[use included minilzo]),
	minilzo_enabled=$withval,
	minilzo_enabled=no)

AC_MSG_CHECKING([whether to use the included lzo compression library])
AC_MSG_RESULT($minilzo_enabled)

LZO_LIBS=
if test "$minilzo_enabled" != "yes"; then
  AC_CHECK_LIB(lzo2, lzo1x_1_compress, LZO_LIBS=-llzo2)
  if test "$LZO_LIBS" = ""; then
    AC_CHECK_LIB(lzo, lzo1x_1_compress, LZO_LIBS=-llzo, [
      minilzo_enabled=yes
      AC_MSG_WARN(
***
*** Could not find liblzo or liblzo2. Will use the included minilzo.
)
    ])
  fi
fi
AC_SUBST(LZO_LIBS)

if test "$LZO_LIBS" = "-llzo"; then
  AC_CHECK_HEADERS(lzo1x.h)
elif test "$LZO_LIBS" = "-llzo2"; then
  AC_CHECK_HEADERS(lzo/lzo1x.h)
fi

if test "$minilzo_enabled" = "yes"; then
  AC_DEFINE(USE_MINILZO, 1, [whether to use the included minilzo])
fi

AM_CONDITIONAL(USE_LZO, test "$use_lzo" = "yes")
AM_CONDITIONAL(ENABLE_INCLUDED_LZO, test "$minilzo_enabled" = "yes")

HAVE_X11=0

if test "x$enable_x11_textmode" = "xyes"; then
	TEMP_LDFLAGS=$LDFLAGS
	LDFLAGS="$HT_LDFLAGS $HT_X11BASE"
	AC_CHECK_LIB(X11, XOpenDisplay, HAVE_X11=1)
	LDFLAGS=$TEMP_LDFLAGS
	if test "x$HAVE_X11" = "x1"; then
		AC_DEFINE(HAVE_TEXTMODE_X11, 1, [This system has Xlib support and we shall use it])
		HT_LIBS="$HT_LIBS -lX11"
		HT_LDFLAGS="$HT_LDFLAGS $HT_X11BASE"
	fi
fi

dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([stdlib.h])
AC_CHECK_HEADERS([string.h])
AC_CHECK_HEADERS([stdint.h])

dnl AC_CONFIG_HEADER(config.h)
CURSES_LIB=
if test "x$NEED_CURSES" = "x1"; then
	AC_CHECK_HEADERS(ncurses.h, AC_DEFINE(CURSES_HDR, <ncurses.h>, [This system provides (n)curses.h/wide]) CURSES_LIB=ncurses)
	if test "x$CURSES_LIB" = "x"; then
		AC_CHECK_HEADERS(curses.h, AC_DEFINE(CURSES_HDR, <curses.h>, [This system provides (n)curses.h]) CURSES_LIB=curses)
		if test "x$CURSES_LIB" = "x"; then
			   AC_MSG_ERROR([*** (n)curses.h needed ***])
		fi
	fi
	HT_LIBS="$HT_LIBS -l$CURSES_LIB"
fi    

dnl Makefile vars.
AC_SUBST(IO_DIR)
AC_SUBST(HT_LDFLAGS)
AC_SUBST(HT_LIBS)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST

AC_CHECK_SIZEOF(char, 1)
AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(long int, 4)

dnl Checks for library functions.
AC_CHECK_FUNCS(asinh acosh atanh)

AC_OUTPUT([
Makefile
analyser/Makefile
asm/Makefile
doc/Makefile
eval/Makefile
info/Makefile
io/Makefile
io/posix/Makefile
io/win32/Makefile
minilzo/Makefile
output/Makefile
tools/Makefile
])

echo 1>&2
echo "$0 successful." 1>&2
echo 1>&2
echo "=====================" 1>&2
echo "Configuration summary" 1>&2
echo "=====================" 1>&2
echo 1>&2
if test "x$HAVE_X11" = "x1"; then
	echo "X11 textmode support available:   yes" 1>&2
else
	echo "X11 textmode support available:   no" 1>&2
fi
echo "enable profiling:                 $enable_profiling" 1>&2
echo "make a release build:             $enable_release" 1>&2
echo "using included minilzo:           $minilzo_enabled" 1>&2
